package com.axxc.jpa.pattendegign.commons;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.axxc.jpa.pattendegign.singleton.SingleA;
import com.axxc.jpa.pattendegign.singleton.SingleB;
import com.axxc.jpa.pattendegign.singleton.SingleC;
import com.axxc.jpa.pattendegign.singleton.SingleD;
import com.axxc.jpa.pattendegign.singleton.SingleE;

/**
 * @ClassName TestSinglePatten
 * @Description //TODO 请描述该类的作用
 * @author xxc
 * @data 2018年8月30日下午9:40:00
 * @see {@link http://gitee.com/biuabiu/ }
 */
public class TestSinglePatten {

	public static final ExecutorService threadPool = Executors.newFixedThreadPool(3);

	public static void main(String[] args) {
		testSingle6();
		threadPool.shutdown();
	}

	/**
	 * 懒汉式 延迟初始化,线程不安全
	 */
	protected static void testSingle1() {
		for (int i = 0; i < 3; i++) {
			Runnable runnable = new Runnable() {
				@Override
				public void run() {
					SingleA.getInstance().show();
				}
			};
			threadPool.submit(runnable);
		}
	}

	/**
	 * 懒汉式 延迟初始化,线程安全
	 */
	protected static void testSingle2() {
		for (int i = 0; i < 3; i++) {
			Runnable runnable = new Runnable() {
				@Override
				public void run() {
					SingleA.getInstance2().show();
				}
			};
			threadPool.submit(runnable);
		}
	}

	/**
	 * 饿汉式,即类加载时初始化,非延迟加载,线程安全
	 */
	protected static void testSingle3() {
		for (int i = 0; i < 3; i++) {
			Runnable runnable = new Runnable() {
				@Override
				public void run() {
					SingleB.getInstance().show();
				}
			};
			threadPool.submit(runnable);
		}
	}

	/**
	 * 双重锁校验模式
	 */
	protected static void testSingle4() {
		for (int i = 0; i < 3; i++) {
			Runnable runnable = new Runnable() {
				@Override
				public void run() {
					SingleC.getInstance().show();
				}
			};
			threadPool.submit(runnable);
		}
	}

	/**
	 * 静态内部类登记式显示调用,延迟加载,线程安全
	 */
	protected static void testSingle5() {
		for (int i = 0; i < 3; i++) {
			Runnable runnable = new Runnable() {
				@Override
				public void run() {
					SingleD.getInstance().show();
				}
			};
			threadPool.submit(runnable);
		}
	}

	/**
	 * 枚举实现单例,非延迟加载,线程安全
	 */
	protected static void testSingle6() {
		for (int i = 0; i < 3; i++) {
			Runnable runnable = new Runnable() {
				@Override
				public void run() {
					SingleE.INSTANCE.getInstance();
				}
			};
			threadPool.submit(runnable);
		}
	}

}
